import 'dart:math';

import 'package:route/route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo2/base/plugin/common_native.dart';
import 'package:flutter_demo2/pages.dart';

class TestRoutePage extends StatelessWidget {
  const TestRoutePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    getRandom();
    return Scaffold(
      appBar: AppBar(title: const Text("test route page")),
      body: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: [
            Text("random == $random"),
            ElevatedButton(
              onPressed: () {
                router.openFlutterPage(FlutterPages.testPage1, args: {"name": "aaa", "age": 999});
              },
              child: const Text("push test page1"),
            ),
            ElevatedButton(
              onPressed: () {
                router.openFlutterPage(FlutterPages.testPage1, openType: OpenType.pushReplacement);
              },
              child: const Text("push replacement test page2"),
            ),
            ElevatedButton(
              onPressed: () {
                router.openFlutterPage(FlutterPages.testPage1, openType: OpenType.popAndPush);
              },
              child: const Text("pop and push test page3"),
            ),
            ElevatedButton(
              onPressed: () {
                router.openFlutterPage(FlutterPages.historyPage);
              },
              child: const Text("route to top-level history page"),
            ),
            ElevatedButton(
              onPressed: () {
                router.openFlutterPage(FlutterPages.settingPage);
              },
              child: const Text("route to top-level setting page"),
            ),
            ElevatedButton(
              onPressed: () {
                router.openFlutterPage(FlutterPages.mePage, needLogin: true);
              },
              child: const Text("route to top-level me page but need login"),
            ),
            ElevatedButton(
              onPressed: () {
                router.openFlutterPage("unknown page");
              },
              child: const Text("route to unknown"),
            ),
            ElevatedButton(
              onPressed: () {
                commonNative.openNativePage();
              },
              child: const Text("open native page by multi engine"),
            ),
            ElevatedButton(
              onPressed: () {
                commonNative.openWebView();
              },
              child: const Text("openWebView(switch web view fragment)"),
            ),
            ElevatedButton(
              onPressed: () {
                commonNative.openNativePage();
              },
              child: const Text("open native page by single engine"),
            ),
          ],
        ),
      ),
    );
  }
}

// random不变，则证明在一个引擎中
bool isFirst = true;
int random = 0;

int getRandom() {
  if (isFirst) {
    random = Random().nextInt(100);
    isFirst = false;
  }
  return random;
}
